<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>职务信息管理</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0"> 
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<jsp:include page="../resource.jsp"></jsp:include>
	<script type="text/javascript">
	var ptId = '';  //单击选中的节点的ID
	$(document).ready(function() {
		var h = '85%';
		if($.browser.msie && ($.browser.version == 8.0 || $.browser.version == 9.0)){
	    	 h = $(self.parent.parent.frames['mainFrame']).height()-87; 
	    }
        $('#role').omGrid({
        	dataSource : 'role!findAllRole.do',
            height : h,
            limit : 20,
            showIndex : true,
            colModel : [ 
                         {header : '角色名称', name : 'name', width : 100, align : 'center'}
                         //,{header : '是否启用', name : 'enable', width : 120, align : 'left'}
                         ]
        });
        
        $('#postformbtn').omButton({});
        
        $('#roleEditor').omDialog({
        	autoOpen: false,
  		    height: 120,
  		    width : 400,
	  		buttons: [{
	              text : "确定", 
	              click : function () {
	                $('#roleForm').submit();
	              }
	           }, {
	              text : "取消", 
	              click : function () {
	                $("#roleEditor" ).omDialog("close");
	            }
	        }]
        });
        
        $('#roleAdd').click(function(){
        	$('#roleEditor').omDialog("open");
        	$('#id').val('');
        	$('#roleForm').resetForm();
        });
        
        $('#roleModify').click(function(){
        	var selects = $("#role").omGrid("getSelections",true);
        	if(selects.length <= 0){
        		$.omMessageBox.alert({content:'请您选择修改的记录'});
        		return;
        	}
        	$('#roleEditor').omDialog("open");
        	$('#roleForm').resetForm();
        	$('#id').val(selects[0].id);
        	$('#name').val(selects[0].name);
        });
        
        $("#roleDelete").click(function(){
        	var selects = $("#role").omGrid("getSelections",true);
        	if(selects.length <= 0){
        		$.omMessageBox.alert({content:'请您选择删除的记录'});
        		return;
        	}
        	 $.omMessageBox.confirm({
                 title:'确认删除',
                 content:'您确定删除该职务？',
                 onClose:function(result){
                	 if(result){
                		 $.ajax({
                			 url : 'role!deleteRole.do',
                			 data : {id:selects[0].id},
                			 success : function(data){
                				 if(data == '1'){
                					 $.omMessageTip.show({timeout : 2000,content : '删除成功！'});
                					 $('#role').omGrid("reload");
                				 }else{
                					 $.omMessageTip.show({timeout : 2000,content : '删除失败！'});
                				 }
                			 }
                		 });
                	 }
                 }
             });
        });
        
        var roleOptions  = {
        		  url : 'role!saveRole.do',
                  success : function(data){
                     	if(data == '1'){  
                         	$.omMessageTip.show({timeout : 2000,content : '保存成功！'});
                         	$('#roleEditor').omDialog('close');
                         	$('#role').omGrid("reload");
                     	}else{
                     		$.omMessageTip.show({timeout : 2000,content : '保存失败！',type : 'error'});
                     	}
                  }
        	  };
      	  $('#roleForm').submit(function() {
                $(this).omAjaxSubmit(roleOptions);
                return false;//返回false,阻止浏览器默认行为
          });
      	  
      	if($('#authorityCatalogTree').children().length <= 0){
    		$('#authorityCatalogTree').omTree({
    	        dataSource : 'catalog!queryList.do',
    	        showCheckbox: true,
    	        widht : 350,
    	        height : 650
    	    });
    	}else{
    		$('#authorityCatalogTree').omTree('collapseAll');
    	}
      	  
      	$('#roleAuthority').click(function(){
      		var selects = $("#role").omGrid("getSelections",true);
        	if(selects.length <= 0){
        		$.omMessageBox.alert({content:'请您选择一个角色'});
        		return;
        	}
    		$('#roleId').val(selects[0].id); //设置职位ID
    		$('#authorityCatalogTree').omTree('checkAll',false);
    		$('#editorAuthority').omDialog('open');
    		$.ajax({
    			  url: "role!findMyCatalog.do",
    			  data : {roleId : selects[0].id},
    			  success: function(data){
    				  if(data == '0') {
    					  return;
    					}
    				  var catalogObj = data.substring(1,data.length - 1).split(',');
    				  for(var i=0 ; i< catalogObj.length ; i++){   //TODO 问题存在，只能选中第一个节点
    					  var target = $('#authorityCatalogTree').omTree("findNodes", "id", catalogObj[i].trim(),'',true);
    				  	if(target) {
    					  	if(target.length > 0){
    					  		$('#authorityCatalogTree').omTree('check',target[0]);
    					  	}
    				  	  }
    				  }
    			  }
    			});
    	});	
      	
      	$('#editorAuthority').omDialog({
  		  autoOpen: false,
  		  height: 450,
  		  width : 280
  		});
      	
      //提交隐藏域
		$('#authorityForm').submit(function() {
			var checkedIds = $('#authorityCatalogTree').omTree('getChecked',true);
			  var ids='';
			  for(var i=0 ; i<checkedIds.length ; i++){
				  ids += checkedIds[i].id+',';
			  }
	        $(this).omAjaxSubmit({
	      	  url : 'role!saveAuthority.do',
	      	  data : {catalogIds:ids,roleId:$('#roleId').val()},
		      success : function(data){
		             	if(data == '1'){
		                 	$('#editorAuthority').omDialog('close');
		                 	$.omMessageTip.show({timeout : 2000,content : '保存成功！'});
		             	}else{
		             		$.omMessageTip.show({timeout : 2000,content : '保存失败！',type : 'error'});
		             	}
		          }
	        });
	        return false;//返回false,阻止浏览器默认行为
	    });
      
		$('#subform').omButton({});
      	
    });
	</script>  
  </head>
  
  <body>
    <div class="toolbar mar">
		         <a href="#" class="rbutton" id="roleAdd"><span><img src="../images/add.gif" width="14" height="14" /><ins>添加</ins></span></a>
		         <a  class="line"></A>
		         <a href="#" class="rbutton" id="roleModify"><span><img src="../images/add.gif" width="14" height="14" /><ins>修改</ins></span></a>
		         <a  class="line"></A>
		         <a href="#" class="rbutton" id="roleDelete"><span><img src="../images/del.gif" width="14" height="14" /><ins>删除</ins></span></a>
		         <a  class="line"></A>
		         <a href="#" class="rbutton" id="roleAuthority"><span><img src="../images/add.gif" width="14" height="14" /><ins>授权</ins></span></a>
	</div>
    <table id="role"></table>
    
    <div id="roleEditor" title="角色职务" style="display: none;">
     <form id="roleForm" method="post">
          <table width="100%" border="0" class="grid_layout" cellspacing="0">
             <tr>
               <td width="20%" align="right">角色名称：</td>
               <td width="68%" colspan="3">
                 <input type="hidden" name="r.id" id="id">
                 <input type="text" name="r.name" id="name" class="input_text"/>
               </td>
             </tr>
           </table>
         </form>
   </div>
   
   <div id="editorAuthority" title="编辑菜单权限" style="display: none;">
	    <form id="authorityForm" method="post">
		     <table width="100%" border="0" class="grid_layout" cellspacing="0">
		       <tr>
		           <td>
		             <input type="hidden" id="roleId"/>
		             <ul id="authorityCatalogTree"></ul>
		           </td>
		       </tr>
		       <tr>
		           <td>
		             <button id="subform" type="submit"> 保&nbsp;&nbsp;&nbsp;&nbsp;存 </button>
		           </td>
		       </tr>
		     </table>
	     </form>
    </div>
  </body>
</html>
